#include<bits/stdc++.h>
using namespace std;
int main()
{
    int T;
    cin >> T;
    for(int q = 0;q < T;q++)
    {
        int v[3000],d[3000],a[3000],w[1000000];
        int n,m,l,s;
        cin >> n >> m >> l >> s;
        for(int i = 0;i < n;i++)
        {
            cin >> v[i] >> d[i] >> a[i];
        }
        for(int i = 0;i < m;i++)
        {
            cin >> w[i];
        }
        int max = 0;
        for(int i = 0;i < m;i++)
        {
            if(max < w[i])
            {
                max = w[i];
            }
        }
        int ans1 = 0;
        for(int i = 0;i <= n;i++)
        {
            if(v[i] > 3 && d[i] < max)
                ans1++;
        }
        if(ans1 == 0)
        {
            cout << m;
        }
        else
        {
            cout << m - 1;
        }
    }
    return 0;

}

